package com.infomanage.service.plan;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.LeaderreservePlanDAO;
import com.infomanage.dao.PersonDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.LeaderreservePlan;
import com.infomanage.po.Person;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.LeaderreservePlanItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

/**
 * 干部储备计划
 * 
 */
public class LeaderReservePlanService {

	public static LeaderreservePlanItem loadLeaderReservePlan(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			String leaderId = request.getParameter("leaderId");
			if (StringUtils.isEmpty(leaderId))
				return null;
			LeaderreservePlanDAO dao = new LeaderreservePlanDAO();
			return dao.find(Integer.parseInt(leaderId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<LeaderreservePlanItem> loadLeaderReservePlanList(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			LeaderreservePlanDAO dao = new LeaderreservePlanDAO();
			List<LeaderreservePlanItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject saveLeaderReservePlan(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			LeaderreservePlan plan = new LeaderreservePlan();
			plan.setCandidatePostion(Integer.parseInt(request.getParameter("candidatePositoin")));
			plan.setCurrentPosition(Integer.parseInt(request.getParameter("currentPosition")));
			plan.setDesciption(request.getParameter("description"));
			plan.setPersonId(Integer.parseInt(request.getParameter("personId")));
			plan.setTimestamp(request.getParameter("timestamp"));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				plan.setDeptId(person.getDeptId());
			}
			LeaderreservePlanDAO dao = new LeaderreservePlanDAO();
			dao.add(plan);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static ReturnObject modifyLeaderReservePlan(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int leaderId = Integer.parseInt(request.getParameter("leaderId"));
			LeaderreservePlanDAO dao = new LeaderreservePlanDAO();
			LeaderreservePlan plan = dao.find(LeaderreservePlan.class, leaderId);
			plan.setCandidatePostion(Integer.parseInt(request.getParameter("candidatePositoin")));
			plan.setCurrentPosition(Integer.parseInt(request.getParameter("currentPosition")));
			plan.setDesciption(request.getParameter("description"));
			plan.setPersonId(Integer.parseInt(request.getParameter("personId")));
			plan.setTimestamp(request.getParameter("timestamp"));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				plan.setDeptId(person.getDeptId());
			}
			dao.modify(plan);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static ReturnObject removeLeaderReservePlan(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String leaderId = request.getParameter("leaderId");
			LeaderreservePlanDAO dao = new LeaderreservePlanDAO();
			dao.remove(LeaderreservePlan.class, Integer.parseInt(leaderId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static List<LeaderreservePlanItem> search(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			LeaderreservePlanDAO dao = new LeaderreservePlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));

			}
			searchBean.setPage(page);
			List<LeaderreservePlanItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject removeBatchLeaderReservePlan(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			LeaderreservePlanDAO dao = new LeaderreservePlanDAO();
			if (batchIds != null) {
				String[] ids = batchIds.split(";");
				for (String id : ids) {
					dao.remove(LeaderreservePlan.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}

	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			LeaderreservePlanDAO dao = new LeaderreservePlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1) / PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String deptId = request.getParameter("deptId");
		String currentPosition = request.getParameter("currentPosition");
		String personId = request.getParameter("personId");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(deptId)) {
			FieldBean field = new FieldBean("deptId", deptId);
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(currentPosition)) {
			FieldBean field = new FieldBean("currentPosition", Integer.parseInt(currentPosition));
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(personId)) {
			FieldBean field = new FieldBean("personId", Integer.parseInt(personId));
			searchBean.add(field);
		}

		// 设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
